<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=2105377167,2778088571&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >开发规范&amp;drf基础</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、web开发模式"><a href="#一、web开发模式" class="headerlink" title="一、web开发模式"></a>一、web开发模式</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 前后端混合开发(前后端不分离):返回的是html的内容，需要写模板。</span><br><span class="line">  </span><br><span class="line"><span class="number">2.</span> 前后端分离:只专注写后端接口，返回json格式，xml格式</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 格式与语言各自区别</span><br><span class="line">    <span class="comment"># xml格式</span></span><br><span class="line">        &lt;xml&gt;</span><br><span class="line">        &lt;name&gt;Ame&lt;/name&gt;</span><br><span class="line">        &lt;/xml&gt;</span><br><span class="line">    <span class="comment"># json格式</span></span><br><span class="line">        &#123;<span class="string">&quot;name&quot;</span>:<span class="string">&quot;Ame&quot;</span>&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"># java---&gt; jsp</span></span><br><span class="line">        https://www.pearvideo.com/category_loading.jsp</span><br><span class="line">    <span class="comment"># php洗的网页</span></span><br><span class="line">    	http://www.aa7a.cn/user.php</span><br><span class="line">    <span class="comment"># python写的</span></span><br><span class="line">    	http://www.aa7a.cn/user.html</span><br><span class="line"><span class="number">4.</span> 动态与静态网页区别</span><br><span class="line">	其实就是一个是要查数据库 一个静止的html</span><br></pre></td></tr></table></figure>

<h2 id="二、API接口"><a href="#二、API接口" class="headerlink" title="二、API接口"></a>二、API接口</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> 什么是API接口</span><br><span class="line">	通过网络，规定了前后端信息交互规则的url连接，也就是前后端信息交互的媒介</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span>比如百度地图的api接口 														https://api.<span class="built_in">map</span>.baidu.com/place/v2/searchak=6E823f587c95f0148c19993539b99295&amp;region=%E4%B8%8A%E6%B5%B7&amp;query=%E8%<span class="number">82</span>%AF%E5%BE%B7%E5%9F%BA&amp;output=xml</span><br></pre></td></tr></table></figure>

<h2 id="三、Postman"><a href="#三、Postman" class="headerlink" title="三、Postman"></a>三、Postman</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">postman是目前最好用的，模拟发送http请求的工具</span></span><br><span class="line"><span class="string">双击安装，安装完自动打开</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br></pre></td></tr></table></figure>

<h2 id="四、Restful十条规范-重点"><a href="#四、Restful十条规范-重点" class="headerlink" title="四、Restful十条规范(重点)"></a>四、Restful十条规范(重点)</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">REST全称是Representational State Transfer， 中文意思是表述(编者注:通常译为表征性状态转移)。它首次出现在2000年Roy Fielding的博士论文中。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">Restful是一种一定Web API接口的设计风格，尤其适用于前后端分离的应用模式中。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">这种风格的理念认为后端开发任务就是提供数据，对外提供的是数据资源的访问接口，所以在定义接口时，客户端访问的URL路径就表示这种要操作的数据资源。</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">事实上，我们可以使用任何一个框架都可以实现符合restful规范的API接口。</span></span><br><span class="line"><span class="string">&quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 10条规范</span></span><br><span class="line">	<span class="number">1.</span>数据的安全保障：url链接一般都采用https协议进行传输(采用https协议，可以提高数据交互过程中的安全性)</span><br><span class="line">    </span><br><span class="line">	<span class="number">2.</span>接口特征表现，一看就知道是个api接口</span><br><span class="line">    	用api关键字标识接口url：</span><br><span class="line">            - [https://api.baidu.com](https://api.baidu.com/)</span><br><span class="line">            - https://www.baidu.com/api</span><br><span class="line">        (看到api字眼，就代表该请求url链接是完成前后台数据交互的)</span><br><span class="line">        </span><br><span class="line">	<span class="number">3.</span>多数据版本共存</span><br><span class="line">    	-在url连接中标识数据版本</span><br><span class="line">    	-https://api.baidu.com/v1</span><br><span class="line">    	-https://api.baidu.com/v1</span><br><span class="line">    	(uel链接中的v1、v1就是不同数据版本的提现(只有在一种数据资源有多版本情况下))</span><br><span class="line">        </span><br><span class="line">	<span class="number">4.</span>数据即是资源，均使用名词(可复数)</span><br><span class="line">    	-接口一般都是完成前后台数据的交互，交互的数据我们称之为资源</span><br><span class="line">            - https://api.baidu.com/users</span><br><span class="line">            - https://api.baidu.com/books</span><br><span class="line">            - https://api.baidu.com/book</span><br><span class="line">        (一般提倡用资源的复数形式，在url链接中奖励不要出现操作资源的动词，错误示范</span><br><span class="line">        https://api.baidu.com/delete-user</span><br><span class="line">        特殊的接口可以出现东西，因为这些借口一般没有一个明确的资源，或是动词就是接口的核心含义</span><br><span class="line">        - https://api.baidu.com/place/search</span><br><span class="line">        - https://api.baidu.com/login)</span><br><span class="line">        </span><br><span class="line">	<span class="number">5.</span>资源操作由请求方式决定(method)</span><br><span class="line">    	- 操作资源一般都会涉及到增删改查，我们提供请求方式来标识增删改查动作</span><br><span class="line">            - https://api.baidu.com/books - get请求：获取所有书</span><br><span class="line">            - https://api.baidu.com/books/<span class="number">1</span> - get请求：获取主键为<span class="number">1</span>的书</span><br><span class="line">            - https://api.baidu.com/books - post请求：新增一本书书</span><br><span class="line">            - https://api.baidu.com/books/<span class="number">1</span> - put请求：整体修改主键为<span class="number">1</span>的书</span><br><span class="line">            - https://api.baidu.com/books/<span class="number">1</span> - patch请求：局部修改主键为<span class="number">1</span>的书</span><br><span class="line">            - https://api.baidu.com/books/<span class="number">1</span> - delete请求：删除主键为<span class="number">1</span>的书</span><br><span class="line">                </span><br><span class="line">	<span class="number">6.</span>过滤，通过url上传参的形式传递搜索条件</span><br><span class="line">    	- https://api.example.com/v1/zoos?limit=<span class="number">10</span>：指定返回记录的数量</span><br><span class="line">    	- https://api.example.com/v1/zoos?offset=<span class="number">10</span>：指定返回记录的开始位置</span><br><span class="line">    	- https://api.example.com/v1/zoos?page=<span class="number">2</span>&amp;per_page=<span class="number">100</span>：指定第几页，以及每页的记录数</span><br><span class="line">    	- https://api.example.com/v1/zoos?sortby=name&amp;order=asc：指定返回结果按照哪个属性排序，以及排序顺序</span><br><span class="line">    	- https://api.example.com/v1/zoos?animal_type_id=<span class="number">1</span>：指定筛选条件</span><br><span class="line">            </span><br><span class="line">	<span class="number">7.</span>响应状态吗</span><br><span class="line">    	<span class="comment"># 正常响应</span></span><br><span class="line">            -响应状态码2xx</span><br><span class="line">                - <span class="number">200</span>：常规请求</span><br><span class="line">                - <span class="number">201</span>：创建成功</span><br><span class="line">    	<span class="comment"># 重定向相应</span></span><br><span class="line">            -响应状态码3xx</span><br><span class="line">                - <span class="number">301</span>：永久重定向</span><br><span class="line">                - <span class="number">302</span>：暂时重定向</span><br><span class="line">    	<span class="comment"># 客户端异常</span></span><br><span class="line">            - 响应状态码4xx</span><br><span class="line">                - <span class="number">403</span>：请求无权限</span><br><span class="line">                - <span class="number">404</span>：请求路径不存在</span><br><span class="line">                - <span class="number">405</span>: 请求方法不存在</span><br><span class="line">    	<span class="comment"># 服务器异常</span></span><br><span class="line">            - 响应状态码5xx</span><br><span class="line">                - <span class="number">500</span>：服务器异常</span><br><span class="line">                </span><br><span class="line">	<span class="number">8.</span> 错误处理，应返回错误信息，error当做key</span><br><span class="line">    	&#123;</span><br><span class="line">            error:<span class="string">&quot;无权限操作&quot;</span></span><br><span class="line">    	&#125;</span><br><span class="line">        </span><br><span class="line">	<span class="number">9.</span> 返回结果，针对不同操作，服务器向用户返回的结果应该符合以下规范</span><br><span class="line">        GET /collection：返回资源对象的列表（数组）</span><br><span class="line">        GET /collection/resource：返回单个资源对象</span><br><span class="line">        POST /collection：返回新生成的资源对象</span><br><span class="line">        PUT /collection/resource：返回完整的资源对象</span><br><span class="line">        PATCH /collection/resource：返回完整的资源对象</span><br><span class="line">        DELETE /collection/resource：返回一个空文档</span><br><span class="line">     <span class="number">10.</span> 需要url请求的资源需要访问资源的请求链接</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            Hypermedia API，RESTful API最好做到Hypermedia，</span></span><br><span class="line"><span class="string">            即返回结果中提供链接，连向其他API方法，使得用户不查文档，也知道下一步应该做什么</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;status&quot;</span>: <span class="number">0</span>,</span><br><span class="line">            <span class="string">&quot;msg&quot;</span>: <span class="string">&quot;ok&quot;</span>,</span><br><span class="line">            <span class="string">&quot;results&quot;</span>:[</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;name&quot;</span>:<span class="string">&quot;肯德基(罗餐厅)&quot;</span>,</span><br><span class="line">                    <span class="string">&quot;img&quot;</span>: <span class="string">&quot;https://image.baidu.com/kfc/001.png&quot;</span></span><br><span class="line">                &#125;</span><br><span class="line">                ...</span><br><span class="line">                ]</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>

<h2 id="五、drf的简单使用"><a href="#五、drf的简单使用" class="headerlink" title="五、drf的简单使用"></a>五、drf的简单使用</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装：pip install djangorestframework==3.10.3</span></span><br><span class="line"><span class="comment"># 使用</span></span><br><span class="line">	<span class="number">1</span> 在setting.py 的app中注册</span><br><span class="line">        INSTALLED_APPS = [</span><br><span class="line">        <span class="string">&#x27;rest_framework&#x27;</span></span><br><span class="line">        ]</span><br><span class="line">        </span><br><span class="line">    <span class="number">2</span> 在models.py中写表模型</span><br><span class="line">    	<span class="class"><span class="keyword">class</span> <span class="title">Book</span>(<span class="params">models.Model</span>):</span></span><br><span class="line">            nid=models.AutoField(primary_key=<span class="literal">True</span>)</span><br><span class="line">            name=models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">            price=models.DecimalField(max_digits=<span class="number">5</span>,decimal_places=<span class="number">2</span>)</span><br><span class="line">            author=models.CharField(max_length=<span class="number">32</span>)</span><br><span class="line">            </span><br><span class="line">    <span class="number">3</span> 新建一个序列化类（听不懂没事）</span><br><span class="line">    	<span class="keyword">from</span> rest_framework.serializers <span class="keyword">import</span> ModelSerializer</span><br><span class="line">        <span class="keyword">from</span> app01.models <span class="keyword">import</span>  Book</span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">BookModelSerializer</span>(<span class="params">ModelSerializer</span>):</span></span><br><span class="line">            <span class="class"><span class="keyword">class</span> <span class="title">Meta</span>:</span></span><br><span class="line">                model = Book</span><br><span class="line">                fields = <span class="string">&quot;__all__&quot;</span></span><br><span class="line">                </span><br><span class="line">    <span class="number">4</span> 在视图中写视图类</span><br><span class="line">        <span class="keyword">from</span> rest_framework.viewsets <span class="keyword">import</span> ModelViewSet</span><br><span class="line">        <span class="keyword">from</span> .models <span class="keyword">import</span> Book</span><br><span class="line">        <span class="keyword">from</span> .ser <span class="keyword">import</span> BookModelSerializer</span><br><span class="line">        <span class="class"><span class="keyword">class</span> <span class="title">BooksViewSet</span>(<span class="params">ModelViewSet</span>):</span></span><br><span class="line">            queryset = Book.objects.<span class="built_in">all</span>()</span><br><span class="line">            serializer_class = BookModelSerializer</span><br><span class="line">            </span><br><span class="line">    <span class="number">5</span> 写路由关系</span><br><span class="line">    	<span class="keyword">from</span> app01 <span class="keyword">import</span> views</span><br><span class="line">        <span class="keyword">from</span> rest_framework.routers <span class="keyword">import</span> DefaultRouter</span><br><span class="line">        router = DefaultRouter()  <span class="comment"># 可以处理视图的路由器</span></span><br><span class="line">        router.register(<span class="string">&#x27;book&#x27;</span>, views.BooksViewSet)  <span class="comment"># 向路由器中注册视图集</span></span><br><span class="line">          <span class="comment"># 将路由器中的所以路由信息追到到django的路由列表中</span></span><br><span class="line">        urlpatterns = [</span><br><span class="line">            path(<span class="string">&#x27;admin/&#x27;</span>, admin.site.urls),</span><br><span class="line">        ]</span><br><span class="line">        <span class="comment">#这是什么意思？两个列表相加</span></span><br><span class="line">        <span class="comment"># router.urls  列表</span></span><br><span class="line">        urlpatterns += router.urls</span><br><span class="line">        </span><br><span class="line">    <span class="number">6</span> 启动，在postman中测试即可</span><br></pre></td></tr></table></figure>

<h2 id="六、views源码"><a href="#六、views源码" class="headerlink" title="六、views源码"></a>六、views源码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># ModelViewSet继承View（django原生View）</span></span><br><span class="line"><span class="comment"># APIView继承了View</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 先读View的源码</span></span><br><span class="line"><span class="keyword">from</span> django.views <span class="keyword">import</span> View</span><br><span class="line"></span><br><span class="line"><span class="comment"># urls.py</span></span><br><span class="line">path(<span class="string">&#x27;books1/&#x27;</span>, views.Books.as_view()),  <span class="comment">#在这个地方应该写个函数内存地址,views.Books.as_view()执行完，是个函数内存地址,as_view是一个类方法，类直接来调用，会把类自动传入</span></span><br><span class="line">放了一个view的内存地址（View--》as_view--》内层函数）</span><br><span class="line"></span><br><span class="line"><span class="comment"># 请求来了，如果路径匹配，会执行，  函数内存地址(request)</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">view</span>(<span class="params">request, *args, **kwargs</span>):</span></span><br><span class="line">    <span class="comment">#request是当次请求的request</span></span><br><span class="line">    self = cls(**initkwargs)  <span class="comment">#实例化得到一个对象，Book对象</span></span><br><span class="line">    <span class="keyword">if</span> <span class="built_in">hasattr</span>(self, <span class="string">&#x27;get&#x27;</span>) <span class="keyword">and</span> <span class="keyword">not</span> <span class="built_in">hasattr</span>(self, <span class="string">&#x27;head&#x27;</span>):</span><br><span class="line">        self.head = self.get</span><br><span class="line">        self.request = request</span><br><span class="line">        self.args = args</span><br><span class="line">        self.kwargs = kwargs</span><br><span class="line">        <span class="keyword">return</span> self.dispatch(request, *args, **kwargs)</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">dispatch</span>(<span class="params">self, request, *args, **kwargs</span>):</span></span><br><span class="line">		<span class="comment">#request是当次请求的request   self是book对象</span></span><br><span class="line">        <span class="keyword">if</span> request.method.lower() <span class="keyword">in</span> self.http_method_names:</span><br><span class="line">            <span class="comment">#handler现在是：</span></span><br><span class="line">            handler=<span class="built_in">getattr</span>(self,<span class="string">&#x27;get&#x27;</span>),你写的Book类的get方法的内存地址</span><br><span class="line">            handler = <span class="built_in">getattr</span>(self, request.method.lower(), self.http_method_not_allowed)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            handler = self.http_method_not_allowed</span><br><span class="line">        <span class="keyword">return</span> handler(request, *args, **kwargs)  <span class="comment">#执行get(request)</span></span><br></pre></td></tr></table></figure>

<h2 id="七、APIviews源码"><a href="#七、APIviews源码" class="headerlink" title="七、APIviews源码"></a>七、APIviews源码</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">```</span><br><span class="line"></span><br><span class="line"><span class="comment">## 八、drf的request类</span></span><br><span class="line"></span><br><span class="line">```python</span><br></pre></td></tr></table></figure>


            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/25/drf------%E5%BC%80%E5%8F%91%E8%A7%84%E8%8C%83&amp;%E5%9F%BA%E7%A1%80/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/25/drf------%E5%BC%80%E5%8F%91%E8%A7%84%E8%8C%83&amp;%E5%9F%BA%E7%A1%80/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
